package com.example.demoJDBC.domain;
 

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
 
public class SortByValueExample {
 
    public static void main(String[] argv) {
 
        Map<String, Integer> unsortMap = new HashMap<>();
        unsortMap.put("z", 10);
        unsortMap.put("b", 5);
        unsortMap.put("a", 6);
        unsortMap.put("c", 20);
        unsortMap.put("d", 1);
        unsortMap.put("e", 7);
        unsortMap.put("y", 8);
        unsortMap.put("n", 99);
        unsortMap.put("g", 50);
        unsortMap.put("m", 2);
        unsortMap.put("f", 9);
 
        System.out.println("Original...");
        System.out.println(unsortMap);
 
        //sort by values, and reserve it, 10,9,8,7,6...   naturalOrder正序  reverseOrder倒序
        Map<String, Integer> result = unsortMap.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
 
 
        //Alternative way
        Map<String, Integer> result2 = new LinkedHashMap<>();
        unsortMap.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
 
        System.out.println("Sorted...");
        System.out.println(result);
        System.out.println(result2);

        //获取map中第二个key值
        System.out.println(unsortMap.keySet().iterator().next());
        System.out.println(unsortMap.keySet().iterator().next());

    }
}